﻿using UnityEngine;

public class ObstacleBlock : MonoBehaviour
{
    public Vector3 size = Vector3.one;
    public Vector3 offset = Vector3.zero;

    public Vector3[] Points { get; private set; } = new Vector3[4];

    public readonly PathPoint[] PathPoints = new PathPoint[4];

    //中心点
    public Vector3 Center;

    //对角长度
    public float Length;

    public float SqrRadius;

    public bool UseRotationAndScale = true;

    // Start is called before the first frame update
    void Start()
    {
        if (UseRotationAndScale)
        {
            Points[0] = transform.TransformPoint(new Vector3(offset.x - size.x * 0.5f, 0, offset.z - size.z * 0.5f));
            Points[1] = transform.TransformPoint(new Vector3(offset.x - size.x * 0.5f, 0, offset.z + size.z * 0.5f));
            Points[2] = transform.TransformPoint(new Vector3(offset.x + size.x * 0.5f, 0, offset.z + size.z * 0.5f));
            Points[3] = transform.TransformPoint(new Vector3(offset.x + size.x * 0.5f, 0, offset.z - size.z * 0.5f));
        }
        else
        {
            Points[0] = new Vector3(offset.x - size.x * 0.5f, 0, offset.z - size.z * 0.5f) + transform.position;
            Points[1] = new Vector3(offset.x - size.x * 0.5f, 0, offset.z + size.z * 0.5f) + transform.position;
            Points[2] = new Vector3(offset.x + size.x * 0.5f, 0, offset.z + size.z * 0.5f) + transform.position;
            Points[3] = new Vector3(offset.x + size.x * 0.5f, 0, offset.z - size.z * 0.5f) + transform.position;
        }

        Refresh(0.2f);

        HunterAstarPath.Active.Add(this);
    }

    private void OnDrawGizmos()
    {
        var upPoints = new Vector3[4];
        var downPoints = new Vector3[4];

        if (UseRotationAndScale)
        {
            upPoints[0] = transform.TransformPoint(new Vector3(offset.x - size.x * 0.5f, size.y * 0.5f, offset.z - size.z * 0.5f));
            upPoints[1] = transform.TransformPoint(new Vector3(offset.x - size.x * 0.5f, size.y * 0.5f, offset.z + size.z * 0.5f));
            upPoints[2] = transform.TransformPoint(new Vector3(offset.x + size.x * 0.5f, size.y * 0.5f, offset.z + size.z * 0.5f));
            upPoints[3] = transform.TransformPoint(new Vector3(offset.x + size.x * 0.5f, size.y * 0.5f, offset.z - size.z * 0.5f));

            downPoints[0] = transform.TransformPoint(new Vector3(offset.x - size.x * 0.5f, -size.y * 0.5f, offset.z - size.z * 0.5f));
            downPoints[1] = transform.TransformPoint(new Vector3(offset.x - size.x * 0.5f, -size.y * 0.5f, offset.z + size.z * 0.5f));
            downPoints[2] = transform.TransformPoint(new Vector3(offset.x + size.x * 0.5f, -size.y * 0.5f, offset.z + size.z * 0.5f));
            downPoints[3] = transform.TransformPoint(new Vector3(offset.x + size.x * 0.5f, -size.y * 0.5f, offset.z - size.z * 0.5f));
        }
        else
        {
            upPoints[0] = new Vector3(offset.x - size.x * 0.5f, size.y * 0.5f, offset.z - size.z * 0.5f) + transform.position;
            upPoints[1] = new Vector3(offset.x - size.x * 0.5f, size.y * 0.5f, offset.z + size.z * 0.5f) + transform.position;
            upPoints[2] = new Vector3(offset.x + size.x * 0.5f, size.y * 0.5f, offset.z + size.z * 0.5f) + transform.position;
            upPoints[3] = new Vector3(offset.x + size.x * 0.5f, size.y * 0.5f, offset.z - size.z * 0.5f) + transform.position;

            downPoints[0] = new Vector3(offset.x - size.x * 0.5f, -size.y * 0.5f, offset.z - size.z * 0.5f) + transform.position;
            downPoints[1] = new Vector3(offset.x - size.x * 0.5f, -size.y * 0.5f, offset.z + size.z * 0.5f) + transform.position;
            downPoints[2] = new Vector3(offset.x + size.x * 0.5f, -size.y * 0.5f, offset.z + size.z * 0.5f) + transform.position;
            downPoints[3] = new Vector3(offset.x + size.x * 0.5f, -size.y * 0.5f, offset.z - size.z * 0.5f) + transform.position;
        }

        Gizmos.DrawLine(upPoints[0], upPoints[1]);
        Gizmos.DrawLine(upPoints[1], upPoints[2]);
        Gizmos.DrawLine(upPoints[2], upPoints[3]);
        Gizmos.DrawLine(upPoints[3], upPoints[0]);


        Gizmos.DrawLine(downPoints[0], downPoints[1]);
        Gizmos.DrawLine(downPoints[1], downPoints[2]);
        Gizmos.DrawLine(downPoints[2], downPoints[3]);
        Gizmos.DrawLine(downPoints[3], downPoints[0]);

        Gizmos.DrawLine(upPoints[0], downPoints[0]);
        Gizmos.DrawLine(upPoints[1], downPoints[1]);
        Gizmos.DrawLine(upPoints[2], downPoints[2]);
        Gizmos.DrawLine(upPoints[3], downPoints[3]);

    }

    private void OnDestroy()
    {
        HunterAstarPath.Active.Remove(this);

        for (int i = 0; i < 4; i++)
        {
            PathPoint.Push(PathPoints[i]);
            PathPoints[i] = null;
        }
    }

    public void Refresh(float skinWeight)
    {
        for (int i = 0; i < Points.Length; i++)
        {
            Points[i] -= new Vector3(0, Points[i].y, 0);
        }

        Center = 0.5f * (Points[0] + Points[2]);

        Length = Vector3.Distance(Points[0], Points[2]);

        for (int i = 0; i < PathPoints.Length; i++)
        {
            var p = Points[i];
            var dir = p - Center;
            var v = PathPoint.Pop();

            v.Value = Center + dir.normalized * (Length * 0.5f + skinWeight);

            v.Block = this;

            PathPoints[i] = v;
        }

        PathPoints[0].DiagonalPoint = PathPoints[2];
        PathPoints[2].DiagonalPoint = PathPoints[0];

        PathPoints[1].DiagonalPoint = PathPoints[3];
        PathPoints[3].DiagonalPoint = PathPoints[1];
    }
}
